<template>
	<view>
		<text class="iconfont icon-setting" style="font-size: 24px;" @click="handleSet"></text>
	</view>
</template>

<script>
	import {getCustomTypeList,getRecordList,batchImport} from '@/api/api.js';
	import {saveFile,fetchFileData} from '@/common/file.js';
	export default {
		name:"settings",
		data() {
			return {
				
			};
		},
		emits:['importSuccess'],
		methods:{
			// 导出数据
			async exportFile(){
				const customTypeList = await getCustomTypeList();
				const recordList = (await getRecordList()).list;
				const obj = {customTypeList,recordList}
				saveFile('myAccounts.txt',obj,(res) => {
					if(res) {
						uni.showToast({
							title:"导出成功"
						})
					}
				})
			},
			// 导入数据
			async importFile() {
				const self = this
				fetchFileData('myAccounts.txt',async(res) => {
					await batchImport(res)
					await self.$emit('importSuccess')
					
				})
			},
			// 数据迁移
			dataMigration() {
				const self = this;
				uni.showModal({
					title:'数据迁移',
					content:'【数据迁移】是指更换设备后将旧设备的账单数据导入新设备中。请在旧设备导出数据后，将文件放到新设备，执行导入操作。',
					success:async function (res) {
							if (res.confirm) {
								uni.showActionSheet({
									itemList: ['导入','导出'],
									success: async function (res) {
										// console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
										switch (res.tapIndex){
											case 0:
												// 导入
												await self.importFile()
												break;
											case 1:
												// 导出
												await self.exportFile()
												break;
										}
									},
								});
							}
						}
				})
			},
			// 显示设置操作按钮
			handleSet() {
				const self = this
				uni.showActionSheet({
					itemList: ['数据迁移'],
					success: function (res) {
						// console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						switch (res.tapIndex){
							case 0:
								self.dataMigration()
								break;
							default:
								break;
						}
					},
				});
			},
		}
	}
</script>

<style>

</style>